package com.airlines.crewassignment.service.crew.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.constant.PositionConstant;
import com.airlines.crewassignment.constant.TechConstant;
import com.airlines.crewassignment.dao.TechMapper;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.Position;
import com.airlines.crewassignment.domain.Tech;
import com.airlines.crewassignment.service.crew.TechService;

/**
 * @author xxxx
 * @date 2018年3月28日 
 * @description
 */

@Service
public class TechServiceImpl implements TechService{
	@Autowired
	private TechMapper techMapper;
	
	static Log log = LogFactory.getLog(TechServiceImpl.class);

	@Override
	public Tech CaptainTechLowestBuilder(String acType) {
		Tech tech = techMapper.getTechByNo(TechConstant.THEC_NO_CAPTION_LOWEST);
		tech.setAcType(acType);
		return tech;
	}


	@Override
	public Tech CopilotTechLowestBuilder(String acType) {
		Tech tech = techMapper.getTechByNo(TechConstant.THEC_NO_COPILOT_LOWEST);
		tech.setAcType(acType);
		return tech;
	}


	@Override
	public Tech getTechLowest(Position position) {
		String dutyCode = position.getDutyCode();
		String rankNo = position.getRankNo();
		int seriaNo  = position.getSerialNo();
		String acType = position.getLinkLine().getAcType();
		
		String techNoLowest = null; //最低授权号
		
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			if(seriaNo == 1){
				//一号位，必须选择责任机长及以上
				techNoLowest = TechConstant.THEC_Z_NO_ZRJZ;
			}else if(seriaNo == 2 
					&& (StringUtils.equals(rankNo, PositionConstant.RANK_NO_A001) || StringUtils.equals(rankNo, PositionConstant.RANK_NO_B001))){
				// 二号位，如果是机长岗位，则必须是航线机长以上
				techNoLowest = TechConstant.THEC_Z_NO_HXJZ;
			}else if(StringUtils.equals(rankNo, PositionConstant.RANK_NO_C001)){
				//第一副驾F4至副驾驶
				techNoLowest = TechConstant.THEC_Z_NO_F4;
			
			}else if(StringUtils.equals(rankNo, PositionConstant.RANK_NO_C002)){
				//第二副驾F2至F4
				techNoLowest = TechConstant.THEC_Z_NO_F2;
			}else{
				techNoLowest = TechConstant.THEC_Z_NO_HXJZ;
			}
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			if(seriaNo == 1){
				//乘务长
				techNoLowest = TechConstant.THEC_Z_NO_C_C003;
			}else if(seriaNo == 2){
				//两舱乘务长
				techNoLowest = TechConstant.THEC_Z_NO_C_C005;
			}else{
				//见习乘务员
				techNoLowest = TechConstant.THEC_Z_NO_C_C013;
			}
		}
		
		
		Tech techLowest = techMapper.getTechByNo(techNoLowest);
		if(techLowest == null){
			log.error("号位"+position.toString()+"计算的最低号位为null！！");
		}
		techLowest.setAcType(acType);
		
		return techLowest;
	}


	@Override
	public Tech getTechHighest(Position position) {
		String dutyCode = position.getDutyCode();
		String rankNo = position.getRankNo();
		int seriaNo  = position.getSerialNo();
		String acType = position.getLinkLine().getAcType();

		String techNoHighest = null;//最高授权号
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			if(seriaNo == 1){
				//一号位，必须选择责任机长及以上,最高可选择检查员
				techNoHighest = TechConstant.THEC_Z_NO_JCY;
			}else if(seriaNo == 2 
					&& (StringUtils.equals(rankNo, PositionConstant.RANK_NO_A001) || StringUtils.equals(rankNo, PositionConstant.RANK_NO_B001))){
				// 二号位，如果是机长岗位，则必须是航线机长以上，最高可选择检查员
				techNoHighest = TechConstant.THEC_Z_NO_JCY;
			}else if(StringUtils.equals(rankNo, PositionConstant.RANK_NO_C001)){
				//第一副驾F4至副驾驶，最高副驾
				techNoHighest = TechConstant.THEC_Z_NO_FJ;
			}else if(StringUtils.equals(rankNo, PositionConstant.RANK_NO_C002)){
				//第二副驾F2至F4
				techNoHighest = TechConstant.THEC_Z_NO_F4;
			}else{
				techNoHighest = TechConstant.THEC_Z_NO_JCY;
			}
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			if(seriaNo == 1){
				//乘务长
				techNoHighest = TechConstant.THEC_Z_NO_C_C001;
			}else if(seriaNo == 2){
				//两舱乘务长
				techNoHighest = TechConstant.THEC_Z_NO_C_C005;
			}else{
				//x见习乘务员
				techNoHighest = TechConstant.THEC_Z_NO_C_C006;
			}
		}
		Tech techHighest = techMapper.getTechByNo(techNoHighest);
		if(techHighest == null){
			System.out.print("");
		}
		techHighest.setAcType(acType);
		return techHighest;
	}


	@Override
	public boolean techHightOrEqualThanOther(Tech thisTech, Tech otherTech) {
		if(thisTech == null || otherTech == null){
			return false;
		}
		
		if(!StringUtils.equals(thisTech.getAcType(), otherTech.getAcType())){
			return false;
		}
		
		if(!StringUtils.equals(thisTech.getTechType(), otherTech.getTechType())){
			return false;
		}
		
		if(!StringUtils.equals(thisTech.getInEx(), otherTech.getInEx())){
			return false;
		}
		
		String idxThis = thisTech.getIdx();
		String idxOther = otherTech.getIdx();
		if(!NumberUtils.isNumber(idxThis) || !NumberUtils.isNumber(idxOther)){
			return false;
		}
		
		int idxThisInt = NumberUtils.toInt(idxThis);
		int idxOtherInt = NumberUtils.toInt(idxOther);
		//Idx 越小，级别越高
		if(idxThisInt <= idxOtherInt){
			return true;
		}
		
		return false;
	}


	@Override
	public boolean CrewTechIsAbleForPositionTech(Crew crew, Position position) {
		List<Tech> techZList = crew.getTechZList();
	
		Tech techLowest = position.getTechLowest();
		Tech techHighest = position.getTechHighest();
		
		for(Tech techZ : techZList){
			if(this.techHightOrEqualThanOther(techZ, techLowest) 
					&& this.techHightOrEqualThanOther(techHighest, techZ) ){
				return  true;
			}
		}
		
		return false;
		
	
	}


	@Override
	public Tech getTechZByPosition(Crew crew, String acType) {
		List<Tech> techZList = crew.getTechZList();
		for(Tech t : techZList){
			if(StringUtils.equals(t.getAcType(),acType)){
				return t;
			}
		}
		return null;
	}


	@Override
	public boolean isTeacher(Tech tech) {
		if(tech == null){
			return false;
		}
		String thchNo = tech.getTechNo();
		if(StringUtils.equals(thchNo, TechConstant.THEC_Z_NO_JYA)
				|| StringUtils.equals(thchNo, TechConstant.THEC_Z_NO_JYB)
				|| StringUtils.equals(thchNo, TechConstant.THEC_Z_NO_JYC)
				){
			return true;
		}
		return false;
	}


	@Override
	public boolean isNewCaption(Tech tech) {
		if(tech == null){
			return false;
		}
		String thchNo = tech.getTechNo();
		if(StringUtils.equals(thchNo, TechConstant.THEC_Z_NO_XJZ)){
			return true;
		}
		return false;
	}
}
